package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService{
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList!=null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                long count = ordersettingDao.findCountById(orderSetting.getOrderDate());
                    if(count>0){
                    ordersettingDao.updata(orderSetting);
                }else {
                    ordersettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String startDate = date+"-1";
        String endDate = date+"-31";
        Map<String,String> map = new HashMap<>();
        map.put("satarDate",startDate);
        map.put("endDate",endDate);
        List<OrderSetting> orderSettings = ordersettingDao.getOrderSettingByMonth(map);
        List<Map> leftobjList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map map2 = new HashMap();
            map2.put("date",orderSetting.getOrderDate().getDate());
            map2.put("number",orderSetting.getNumber());
            map2.put("reservations",orderSetting.getReservations());
            leftobjList.add(map2);
        }
        return leftobjList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        if(ordersettingDao.findCountById(orderSetting.getOrderDate())>0){
            ordersettingDao.updata(orderSetting);
        }else {
            ordersettingDao.add2(orderSetting);
        }
    }
}
